package sum

import model.{Classes, Grade, Lesson, Student}

/**
  * author: HuaZhe Ray
  *
  * describe: TODO
  *
  * createDate: 2017/12/7
  * createTime: 19:11
  *
  */
object SumUtil {

  /**
    * 计算总分前N名
    */
  def beforeTotal(students:List[Student], rank:Int): List[Student] = {
    //var studentS = students.sortWith((s,t)=> if(s.totalPoints() > t.totalPoints()) true else false )
    /*var studentS = students.sortWith((s,t)=> s.totalPoints() > t.totalPoints())

    studentS.take(rank)*/
    //val sortedWith = users.sortWith{case(user1,user2) => user1._2 < user2._2} //List(("XSDYM",23),("HomeWay",25))
    students.sortWith((s,t)=> s.totalPoints() > t.totalPoints()).take(rank)
  }
  //  优化方法
  /*def beforeTotal1(students: List[Student], rank:Int) : List[Student] = students.sortBy(_.totalPoints).take(rank)*/

  /**
    * 计算单科前N名
    * @param students
    * @return
    */
  def beforeSingle(students:List[Student], less:Lesson, rank:Int): List[Student] = {
    /*var studentS = singleRank(students,less)
    studentS.take(rank)*/

    singleRank(students,less).take(rank)

  }


   /**
    * 单科排名 sort
    * @param students
    */
  def singleRank(students:List[Student],less:Lesson) = {
      val result = less.name match {
        /*case "语文" => students.sortWith((s,t)=> if(s.chinesePoints > t.chinesePoints ) true else false )
        case "数学" => students.sortWith((s,t)=> if(s.mathPoints > t.mathPoints )   true else false )
        case "英语" => students.sortWith((s,t)=> if(s.englishPoints > t.englishPoints )true else false )
        使用更好的写法
        */
        case "语文" => students.sortWith((s,t)=> s.chinesePoints > t.chinesePoints )
        case "数学" => students.sortWith((s,t)=> s.mathPoints > t.mathPoints )
        case "英语" => students.sortWith((s,t)=> s.englishPoints > t.englishPoints )
      }
      result
  }

   /**
    *  计算平均分
    */
    def averageSingle(lesson: List[Lesson]): Double = {
          val points:List[Int] = for{
                        s <- lesson
                       }yield s.point
          val totalPoints:Double = points.sum
          val totalSum:   Double = points.length
        //保留2位小数
       (totalPoints/totalSum).formatted("%.2f").toDouble
    }

  /**
    *   计算每科 等级比例
    *
    *  1、尽量使用合适的数据类型，BigDecimal 比 String更好。
       2、可以使用 immutable的，优先使用
    *
    *
    */
    def averageRank(lesson: List[Lesson]) = {
        //       val lsA = lesson.filter(_.rank=='A')
        val lsA = lesson.filter((s:Lesson) => s.rank=='A')
        val lsB = lesson.filter(_.rank=='B')
        val lsC = lesson.filter(_.rank=='C')
        val lsD = lesson.filter(_.rank=='D')
    /*
        //不合适的写法
        val percentA = (lsA.size.toDouble/lesson.size.toDouble)*100
        val percentB = (lsB.size.toDouble/lesson.size.toDouble)*100
        val percentC = (lsC.size.toDouble/lesson.size.toDouble)*100
        val percentD = (lsD.size.toDouble/lesson.size.toDouble)*100
    */
      val percentA = BigDecimal(lsA.size*100/lesson.size).setScale(2, BigDecimal.RoundingMode.HALF_UP)
      val percentB = BigDecimal(lsB.size*100/lesson.size).setScale(2, BigDecimal.RoundingMode.HALF_UP)
      val percentC = BigDecimal(lsC.size*100/lesson.size).setScale(2, BigDecimal.RoundingMode.HALF_UP)
      val percentD = BigDecimal(lsD.size*100/lesson.size).setScale(2, BigDecimal.RoundingMode.HALF_UP)

         //不合适的写法
//       val scores = scala.collection.mutable.Map("A"->(percentA.formatted("%.2f")+"%"),"B"->(percentB.formatted("%.2f")+"%"),"C"->(percentC.formatted("%.2f")+"%"),"D"->(percentD.formatted("%.2f")+"%"))
         val scores = Map("A"->(percentA+"%"),"B"->(percentB+"%"),"C"->(percentC+"%"),"D"->(percentD+"%"))
      scores
    }

    /**
    * 获取年级所有学生
    * @param grade
    * @return   暂时硬编码
    */
    def getGradeStudent(grade:Grade): List[Student] = {
      val classes:List[Classes] = grade.classes
      val students:List[Student] = classes(0).students ++ classes(1).students
      students
    }


}
